﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.Security;
using System.Data;

using FSViet.Survey.Helpers;

namespace FSViet.Survey.Administration.UserSiteMapping
{
	public partial class Assign : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (Request.QueryString["Id"] == null)
				Response.Redirect("~/Administration/Site/List.aspx");
		}

		protected void SaveButton_Click(object sender, EventArgs e)
		{
			Guid userId = Guid.Empty;

			var user=Membership.GetUser(UserNameInput.Text.Trim());
			if (user == null)
			{
				msg.Text = "Not found user: " + UserNameInput.Text.Trim();
				return;
			}
			else
			{
				var siteId = new Guid(Request.QueryString["Id"]);
				try
				{
					var currentUser = Membership.GetUser();
					DataHelper dataH = new DataHelper();
					dataH.Execute("INSERT INTO UserSiteMappings(Id, UserId, SiteId, Description, Created, CreatedId, Modified, ModifiedId) "
						+"VALUES(@Id, '"+user.ProviderUserKey.ToString()+"', @SiteId, @Description, @Created, @CreatedId, @Modified, @ModifiedId)",
						dataH.CreateParam("Id", System.Data.SqlDbType.Char, GlobalApplication.GetGuidS(Guid.NewGuid())),
						dataH.CreateParam("SiteId", System.Data.SqlDbType.Char, GlobalApplication.GetGuidS(siteId)),
						dataH.CreateParam("Description", System.Data.SqlDbType.NVarChar, DescriptionInput.Text.Trim()),
						dataH.CreateParam("Created", System.Data.SqlDbType.Char, GlobalApplication.NowS),
						dataH.CreateParam("CreatedId", System.Data.SqlDbType.Char, GlobalApplication.GetGuidS((Guid)currentUser.ProviderUserKey)),
						dataH.CreateParam("Modified", System.Data.SqlDbType.Char, GlobalApplication.NowS),
						dataH.CreateParam("ModifiedId", System.Data.SqlDbType.Char, GlobalApplication.GetGuidS((Guid)currentUser.ProviderUserKey)));

					Response.Redirect("../Site/Details.aspx?Id=" + Request.QueryString["Id"]);
				}
				catch (Exception ex)
				{
					msg.Text = ex.Message;
#if DEBUG
				msg.Text = ex.ToString();
#endif
				}
			}
		}
	}
}